package org.jeecg.modules.yw.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jeecg.modules.yw.entity.EnterPrise;
import org.jeecg.modules.yw.entity.Order;
import org.jeecg.modules.yw.result.Result;
import org.jeecg.modules.yw.service.EnterPriseService;
import org.jeecg.modules.yw.service.OrderService;
import org.jeecg.modules.yw.service.SysAnnoucementService;
import org.jeecg.modules.yw.vo.SysAnnoucementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "消息")
@RequestMapping("/app/message")
public class MessageController {
	@Autowired
	private SysAnnoucementService annouceService;
	
    @Resource
    private EnterPriseService enterPriseService;
    
    @Resource
    private OrderService orderService;
    
    @Resource
    private SysAnnoucementService sysAnnoucementService;
	
    @ApiOperation(value = "得到所有消息")
    @GetMapping("/getAllMessage")
    public Result getAllMessages() {
    	 List<Map<String, String>> list = annouceService.getMsgList();
    	 return Result.success(list);
    }    
    
    
    @ApiOperation(value = "获取企业列表(注册)")
    @GetMapping("/getRegisteredEnt")
    public Result page(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(name = "roleId") Integer roleId) {
        QueryWrapper<EnterPrise> queryWrapper = new QueryWrapper<>();
        if (roleId == 2) {
        	queryWrapper.ne("isLeader", "2");
        }
        queryWrapper.eq("approve", "0");
        queryWrapper.eq("delFlag", "0");
        if (roleId != null) {
        	queryWrapper.eq("roleId", roleId);
        }
        queryWrapper.orderByDesc("createTime");
        Page<EnterPrise> page = new Page<>(pageNo, pageSize);
        IPage<EnterPrise> result = enterPriseService.page(page, queryWrapper);
        return Result.success(result);
    }
    
    
    @ApiOperation(value = "获取新订单列表")
    @GetMapping("/getNewOrders")
    public Result getNewOrders(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderStatus", "0");
        queryWrapper.eq("delFlag", "0");
        queryWrapper.orderByDesc("createTime");
        Page<Order> page = new Page<>(pageNo, pageSize);
        IPage<Order> result = orderService.page(page, queryWrapper);
        return Result.success(result);
    }
    
    @ApiOperation(value = "获取消息列表")
    @GetMapping("/getMsgs")
    public Result getNewOrders(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, @RequestParam(name = "msgType") Integer msgType, @RequestParam(name = "busiType",  required = false) Integer busiType) {
        QueryWrapper<SysAnnoucementVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("m.msg_type", msgType);
        queryWrapper.eq("m.del_flag", "0");
        if (busiType != null) {
        	 queryWrapper.eq("m.bus_type", busiType);
        }
        queryWrapper.orderByDesc("m.create_time");
        Page<SysAnnoucementVo> page = new Page<>(pageNo, pageSize);
        IPage<SysAnnoucementVo> result = sysAnnoucementService.pageEnter(page, queryWrapper);
        return Result.success(result);
    }    
    
    @ApiOperation(value = "确认消息")
    @PostMapping("/confirmMsg")
    public Result confirmMsg(@RequestParam(name = "id") String id) {        
    	sysAnnoucementService.updateMsg(id);
        return Result.success("ok");
    }
}
